﻿
using HPIT_MVC.Models;
using HPIT_MVC.ViewModel;
using MPMS.DTOModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HPIT_MVC.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        public ActionResult Index()
        {
            var db = new StudentEntity();
            var list = db.Student.Where(s => true)

                .Select(s => new StudentViewModel()
                {
                    Mobile = s.Mobile,
                    Sex = s.Sex,
                    StudentAddress = s.StudentAddress,
                    StudentName = s.StudentName,
                    StudentID = s.StudentID,
                    ClassName = s.Class.ClassName
                })
                .ToList();
            return View(list);
        }


        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.StuList = GetList();
            return View();
        }

        private List<SelectListItem> GetList()
        {
            using (var db = new StudentEntity())
            {
                var stuList = db.Class.Where(c => true).Select(c => new SelectListItem()
                {
                    Text = c.ClassName,
                    Value = c.ClassID.ToString()
                }).ToList();
                return stuList;
            }

        }

        [HttpPost]
        public ActionResult Add(StudentAddViewModel viewModel)
        {
            ViewBag.StuList = GetList();
            using (var db = new StudentEntity())
            {
                var model = new Student()
                {
                    Sex = viewModel.Sex,
                    Mobile = viewModel.Mobile,
                    StudentAddress = viewModel.StudentAddress,
                    StudentName = viewModel.StudentName,
                    ClassId = viewModel.ClassId
                };
                db.Student.Add(model);
                if (db.SaveChanges() > 0)
                {
                    ViewData["Msg"] = "添加成功";
                    //return RedirectToAction("Index");
                }
                else
                {
                    ViewData["Msg"] = "添加失败";
                }
            }
            return View();
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {
            ViewBag.StuList = GetList();
            using (var db = new StudentEntity())
            {
                var model = db.Student.FirstOrDefault(c => c.StudentID == id);
                if (model != null)
                {
                    var viewModel = new StudentEditViewModel()
                    {
                        Mobile = model.Mobile,
                        Sex = model.Sex,
                        StudentName = model.StudentName,
                        StudentAddress = model.StudentAddress,
                        ClassId = model.ClassId,
                        StudentID = model.StudentID
                    };
                    return View(viewModel);
                }
                return HttpNotFound();
            }
        }

        [HttpPost]
        public ActionResult Edit(StudentEditViewModel viewModel)
        {
            ViewBag.StuList = GetList();
            using (var db = new StudentEntity())
            {
                var model = db.Student.FirstOrDefault(c => c.StudentID == viewModel.StudentID);
                if (model != null)
                {
                    model.Mobile = viewModel.Mobile;
                    model.Sex = viewModel.Sex;
                    model.StudentAddress = viewModel.StudentAddress;
                    model.StudentName = viewModel.StudentName;
                    model.ClassId = viewModel.ClassId;

                    db.SaveChanges();
                    ViewData["Msg"] = "学生信息修改成功";
                }
                else
                {
                    ViewData["Msg"] = "网络问题，请稍后重试^^^^";
                }
            }

            return HttpNotFound();
        }

 
        public ActionResult Delete(int sid)
        {
            using (var db=new StudentEntity())
            {
                var result = new OperateResult();
                var model = db.Student.FirstOrDefault(s => s.StudentID == sid);
                if (model != null)
                {
                    db.Student.Remove(model);
                    if (db.SaveChanges() > 0)
                    {
                        result.Success = true;
                        result.Error = "删除成功";
                    }
                    else
                    {
                        result.Success = false;
                        result.Error = "用户信息已被删除，请稍后重试^^^^^";
                    }
                }

                return Json(result);

            }
        }
    }
}